package org.oslims.server.service.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.hibernate.Session;
import org.oslims.domain.LabStaff;
import org.oslims.domain.Report;
import org.oslims.domain.Request;
import org.oslims.server.dao.LabStaffDao;
import org.oslims.server.dao.ReportDao;
import org.oslims.server.dao.RequestDao;
import org.oslims.server.dao.hbm.HbmLabStaffDao;
import org.oslims.server.dao.hbm.HbmReportDao;
import org.oslims.server.dao.hbm.HbmRequestDao;
import org.oslims.server.dao.util.HibernateUtil;

public class FileUploadService extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -910169528897744411L;

	private ReportDao reportDao;
	private RequestDao requestDao;
	private LabStaffDao labStaffDao;

	public FileUploadService() {
		reportDao = new HbmReportDao();
		requestDao = new HbmRequestDao();
		labStaffDao = new HbmLabStaffDao();
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		System.out.println("Bsdfhs");

		response.setContentType("text/html");
		try {
			List<FileItem> items = getFileItems(request);
			Long requestId = null;
			Long labStaffId = null;

			for (FileItem fi : items) {
				if (fi.getFieldName().equals("requestId")) {
					requestId = Long.parseLong(fi.getString());
				}

				if (fi.getFieldName().equals("labStaffId")) {
					labStaffId = Long.parseLong(fi.getString());
				}
			}

			if (requestId != null && labStaffId != null) {
				saveReportForRequest(requestId, labStaffId, items.get(0));
			}

			response.getWriter().write(new String("Uploading Completed...."));
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}

	private void saveReportForRequest(Long requestId, Long labStaffId, FileItem fileItem) {
		Session session = HibernateUtil.getSession();

		reportDao.setSession(session);
		requestDao.setSession(session);
		labStaffDao.setSession(session);

		session.beginTransaction();

		Request request = requestDao.findById(requestId, false);
		LabStaff labStaff = labStaffDao.findById(labStaffId, false);

		Report r = new Report();
		r.setWrittenAt(new Date());
		r.setWrittenBy(labStaff);
		r.setRequest(request);
		r.setContentType(fileItem.getContentType());
		r.setFileName(fileItem.getName());
		r.setContent(fileItem.get());

		reportDao.makePersistent(r);

		session.getTransaction().commit();
		session.close();

	}

	@SuppressWarnings("unchecked")
	private List<FileItem> getFileItems(final HttpServletRequest request) {
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);

		try {
			List<FileItem> items = upload.parseRequest(request);
			return items;
		} catch (Exception e) {
			System.out.println(e.toString());
			e.printStackTrace();
			return null;
		}
	}
}
